<?php
namespace App\Domain;

use App\Model\Area as ModelArea;

class Area {

    //按条件获取区域
    public function getArea( $condition ) {
        $model = new ModelArea();
        return $model->getArea( $condition );
    }

    //按条件获取地址
    public function getAddressList( $user_id ) {
        $model = new ModelArea();
        $data = $model->getAddressList( $user_id );
        //根据省份等id获取名称
        foreach ( $data as $k => &$v ){
            $province = $model->getAreaInfo( $v['province'] );
            $city = $model->getAreaInfo( $v['city'] );
            $district = $model->getAreaInfo( $v['district'] );
            $v['province_name'] = $province['name'];
            $v['city_name'] = $city['name'];
            $v['district_name'] = $district['name'];
        }
        return $data;
    }

    //添加地址
    public function insertAddress( $data ) {
        $model = new ModelArea();
        if( $data['is_default'] == 1 ){
            return $model->insertAddressDefult( $data );
        }
        return $model->insertAddress( $data );
    }

    //修改地址
    public function editAddress( $user_id, $address_id, $data ) {
        $model = new ModelArea();
        if( $data['is_default'] == 1 ){
            $model->editAddressDefult( $user_id, $address_id, $data );
        }
        return $model->editAddress( $user_id, $address_id, $data );
    }

    //删除地址
    public function addressDel( $user_id, $address_id ) {
        $model = new ModelArea();
        $data = $model->addressMessage( $address_id );
        if( $data ){
            if( $data['is_default'] ){
                $model->addressDel( $user_id, $address_id );
                return $model->addressChange( $user_id );
            }else{
                return $model->addressDel( $user_id, $address_id );
            }
        }else{
            return false;
        }
    }

}
